package com.example.day_20.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.day_20.DataBaseUtils.DbUtils;
import com.example.day_20.R;
import com.example.day_20.adapter.RlvHomeAdapter;
import com.example.day_20.bean.InFoBean;
import com.example.day_20.contract.HomeContract;
import com.example.day_20.presenter.HomePresenter;

public class HomeFragment extends Fragment implements HomeContract.View {
    private RecyclerView mRecycler;
    private RlvHomeAdapter rlvHomeAdapter;
    private int mPosi;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    private void initData() {
        HomePresenter homePresenter = new HomePresenter(this);
        homePresenter.getData();
    }

    private void initView(@NonNull final View itemView) {
        mRecycler = (RecyclerView) itemView.findViewById(R.id.recycler);

        registerForContextMenu(mRecycler);
        mRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecycler.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        rlvHomeAdapter = new RlvHomeAdapter(getContext());
        mRecycler.setAdapter(rlvHomeAdapter);

        rlvHomeAdapter.setOnItemLongClickListener(new RlvHomeAdapter.onItemLongClickListener() {
            @Override
            public void onLongClick(View v, int position) {
                mPosi = position;
            }
        });
    }

    @Override
    public void onSuccess(InFoBean inFoBean) {
        rlvHomeAdapter.setDatas(inFoBean.getData().getDatas());
        Log.d("111", "onSuccess: "+inFoBean.getData().getDatas());
    }

    @Override
    public void onFail(String errorMsg) {
        Toast.makeText(getContext(), ""+errorMsg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(1,1,1,"收藏");
        menu.add(1,2,2,"修改");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                InFoBean.DataBean.DatasBean datasBean = rlvHomeAdapter.datas.get(mPosi);
                String title = datasBean.getTitle();
                String url = datasBean.getEnvelopePic();
                DbUtils.insert(title,url);
                Toast.makeText(getContext(), "收藏成功", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                break;
        }
        return super.onContextItemSelected(item);
    }
}
